package com.asset.common.utils;

import javax.servlet.http.HttpServletRequest;

public class DomainUtil {
    public static String getRootDomain(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        String uri = request.getRequestURI();
        String domain = url.delete(url.length() - uri.length(), url.length()).toString();
        int pos = domain.lastIndexOf(".");
        if (pos == -1) {
            return domain;
        } else {
            String temp = domain.substring(0, pos);
            int second = temp.lastIndexOf(".");
            if (second == -1) {
                int pos1 = domain.indexOf("://");
                return pos1 != -1 ? domain.substring(pos1 + 3) : domain;
            } else {
                return domain.substring(second + 1);
            }
        }
    }

    public static String getDomain(HttpServletRequest request){
        return request.getServerName();
    }
}
